package com.jdy.hotel.utils;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;

/**
 * Description: Tools
 * Created by Administrator on 2019/9/14 12:25
 */
public class CollectionUtils {

    /**
     * 新建一个HashSet
     *
     * @param <T>      集合元素类型
     * @param elements 元素数组
     * @return HashSet对象
     */
    @SafeVarargs
    public static <T> HashSet<T> newHashSet(T... elements) {
        return newHashSet(false, elements);
    }

    /**
     * 新建一个HashSet
     *
     * @param <T>      集合元素类型
     * @param isSorted 是否有序，有序返回 {@link LinkedHashSet}，否则返回 {@link HashSet}
     * @param elements 元素数组
     * @return HashSet对象
     */
    @SafeVarargs
    public static <T> HashSet<T> newHashSet(boolean isSorted, T... elements) {
        if (null == elements) {
            return CheckUtil.checkValue(new HashSet<>(), isSorted, new LinkedHashSet<>());
        }
        int initialCapacity = Math.max((int) (elements.length / .75f) + 1, 16);
        HashSet<T> set = isSorted ? new LinkedHashSet<T>(initialCapacity) : new HashSet<T>(initialCapacity);
        Collections.addAll(set, elements);
        return set;
    }

    public static <T> boolean isEmpty(Collection<T> collection) {
        return null == collection || collection.isEmpty();
    }

    public static <K, V> boolean isEmpty(Map<K, V> collection) {
        return null == collection || collection.isEmpty();
    }

    public static <K, V> V computeIfAbsent(Map<K, V> map, K key, Convert<K, V> convert) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            return map.computeIfAbsent(key, v -> convert.apply(key));
        }

        V value;
        if (null == (value = map.get(key))) {
            V newValue = convert.apply(key);
            if (newValue != null) {
                map.put(key, newValue);
                return newValue;
            }
        }
        return value;
    }

    public interface Convert<T, R> {
        R apply(T t);
    }



}
